<?php

/**
 * Description of wlt
 *
 * @author zhangyazhong By 2013-5-23 16:40:17
 * 
 * Info：万里通接口
 */
class api_wlt{

    public static $_host = array(
        'PRODUCT' => WANLITONG_URL,
        'STG' => WANLITONG_POINT_URL,
    );
    private static $_header = array();
    private static $_wltkey = '09FB84B1-D90E-4C14-84DB-DEE924A87B51';
    private static $_productid = array(
        'PRODUCT' => '430110010180108',
        'STG' => '20130527',
    );

    private static function creatParam($param = array()) {
        $param['authType'] = 'md5';
        ksort($param);
        $str = '';
        foreach ($param as $k => $v) {
            $str .= $k . $v;
        }
        $str .= self::$_wltkey;
        $param['sign'] = md5($str);
        return $param;
    }

    /**
     * 获取用户积分
     * @param type $memberid
     * @return type
     */
    public static function getUserPoint($memberid) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/availPointSch.do';
        $param = array(
            'memberId' => $memberid
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 查询用户五项信息
     * @param type $memberid
     * @return type
     */
    public static function getUserFive($memberid) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/memberFiveInfor.do';
        $param = array(
            'memberId' => $memberid
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 获取短信校验码
     * @param type $mobile
     * @return type
     */
    public static function getOptCode($mobile) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/getOTPCode.do';
        $param = array(
            'mobileNo' => $mobile,
            'yanzheng' => utf8_encode('.'),
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 校验短信验证码
     * @param type $mobile
     * @return type
     */
    public static function validateOptCode($mobile, $code, $time) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/validateOTPCode.do';
        $param = array(
            'otpCode' => $code,
            'mobileNo' => $mobile,
            'sendOTPTime' => $time,
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 扣除万里通用户积分
     * @param type $delpoint
     * @return boolean
     */
    public static function delUserPoint($memberid, $delpoint = 0) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/createOrder.do';
        $param = array(
            'memberId' => $memberid,
            'productId' => self::$_productid[ENVIRONMENT],
            'exchangeNum' => $delpoint,
            'ppOrderId' => self::getOrderId(),
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 万里通退货接口
     * @param type $memberid
     * @param type $orderid
     * @return type
     */
    public static function refundPoint($memberid, $orderid) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/returnOrder.do';
        $param = array(
            'memberId' => $memberid,
            'orderId' => $orderid,
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }

    /**
     * 发起中奖短信
     * @param type $p
     * @return type
     */
    public static function lotteryWin($p) {
        $url = self::$_host[ENVIRONMENT] . '/lotteryInter/sendPrizeSms.do';
        $param = array(
            'mobileNo' => $p['userMobile'], //手机号码
            'qishu' => $p['qishu'], //期数
            'couponCode' => $p['couponCode'],
            'prize' => $p['prize'], //中奖金额
        );
        return self::curl($url, self::creatParam($param), self::$_header);
    }
    protected static function curl($url, $param, $header = array(), $method = 'POST') {
        $curl = new framework_base_curl();
        $curl->url = $url;
        $curl->method = $method;
        $curl->params = $param;
        $curl->headers = $header;
        $curl->exec();
        $rs_curl = $curl->result;
        return framework_static_common::str_empty($rs_curl) ? FALSE : json_decode($rs_curl, TRUE);
    }
    
    /**
     * 生成统一订单号码 22位
     * @return type
     */
    protected static function getOrderId() {
        return date('YmdHis') . substr(microtime(), 2, 4) . rand(1000, 9999);
    }
}